package com.chen.gulimall.gulimallproduct.web;

import com.chen.gulimall.gulimallproduct.entity.CategoryEntity;
import com.chen.gulimall.gulimallproduct.service.CategoryService;
import com.chen.gulimall.gulimallproduct.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @GetMapping({"/","/index"})
    public String index(Model model) {
        List<CategoryEntity> data = categoryService.queryLevel1Categorys();

        model.addAttribute("categorys",data);
        //此次thymeleaf会自动进行路径的拼接
        return "index";
    }

    //如果不加此注解则返回会呢thymeleaf解析为前端页面
    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String,List<Catelog2Vo>> getCatagorysJson() {
        Map<String,List<Catelog2Vo>> map = categoryService.getCatagorysJson();

        return map;
    }
}
